<template>
  <!-- 订单详情 -->
  <view class="orderDetail">
    <topBar tabTitle="订单详情"/>
    <!-- <pre>{{ this.orderInfo }}</pre> -->
    <view class="page-nav">
      <flex class="uni-form-item uni-column" justify="space-between">
        <view class="uni-label">订单id：</view>
        <view class="uni-input-text">
          {{ this.orderInfo.id }}
        </view>
      </flex>
      <flex class="uni-form-item uni-column" justify="space-between">
        <view class="uni-label">下单号码: </view>
        <view class="uni-input-text orderNumList-item">
          {{ this.orderInfo.orderNumListStr.toString() }}
        </view>
      </flex>
      <flex class="uni-form-item uni-column" justify="space-between">
        <view class="uni-label">订单状态：</view>
        <view class="uni-input-text">
          {{ orderInfo.periodsNumStatus }}
        </view>
      </flex>
      <flex 
        class="uni-form-item uni-column"
        justify="space-between"
        v-if="orderInfo.periodsNumStatus == '已开奖'"
      >
        <view class="uni-label">中奖状态：</view>
        <view class="uni-input-text">
          {{ winFilterComputed }}
        </view>
      </flex>
      <flex 
        class="uni-form-item uni-column"
        justify="space-between"
        v-if="orderInfo.periodsNumStatus == '已开奖'"
      >
        <view class="uni-label">中奖金额：</view>
        <view class="uni-input-text">
          ￥{{ moneyWin }}
        </view>
      </flex>
      <flex class="uni-form-item uni-column" justify="space-between">
        <view class="uni-label">订单金额：</view>
        <view class="uni-input-text">
          ￥{{ orderInfo.money }}
        </view>
      </flex>
      <flex class="uni-form-item uni-column" justify="space-between">
        <view class="uni-label">订单下单时间：</view>
        <view class="uni-input-text">
          {{ timestampToTime(orderInfo.updataTime) }}
        </view>
      </flex>
      <!-- 操作 -->
      <button type="primary" @click="myClick('打印订单')">
        打印订单
      </button>
    </view>
    <!-- {{ printContent }} -->
    <!-- 蓝牙打印 -->
    <uni-popup ref="bluetoothPrintingPopup" type="bottom" background-color="#fff">
      <bluetoothPrinting ref="bluetoothPrinting" :status="status" :content="printContent" />
    </uni-popup>
  </view>
</template>
<script>
import {
  getAppSetting,
  getOrderSetting,
  getOrderList
} from '../../utils/appStorage'

export default {
  name: "orderDetail",
  data() {
    return {
      AppSetting: {},
      OrderSetting: {},
      orderInfo: {},
      status: 2,
      // printContent: '',
      // 历史下单数据
      OrderStatisticsList: [],
      moneyWin: ''
    };
  },
  components: {},
  onLoad(options) {
    getAppSetting(data => {this.AppSetting = data})
    getOrderSetting(data => {this.OrderSetting = data})
    let query = JSON.parse(decodeURIComponent(options.query));
    this.orderInfo = query
    console.log('query----', query)
    this.OrderStatisticsInit()
  },
  computed: {
    printContent() {
      return `\r\n\r\n\r\n----------------------------\r\n\r\n\r\n订单编号：${this.orderInfo.id}\r\n\r\n下单号码: ${this.orderInfo.orderNumListStr.toString()}\r\n\r\n订单金额：￥${this.orderInfo.money}\r\n\r\n订单状态：${this.orderInfo.periodsNumStatus}${this.orderInfo.periodsNumStatus == '已开奖'?`\r\n\r\n中奖状态：${this.winFilterComputed}\r\n\r\n中奖金额：￥${this.moneyWin}`:''}\r\n\r\n订单下单时间：${this.timestampToTime(this.orderInfo.updataTime)}\r\n\r\n\r\n----------------------------\r\n\r\n\r\n`
    },
    winFilterComputed() {
      let status = '未中奖'
      let moneyWin = 0
      // 小标
      let twoSingleSectionsList = this.orderInfo.orderNumTwo
      // 大标
      let fourSingleSectionsList = this.orderInfo.orderNumFour
      // 中奖号码遍历判断
      let orderSelectList = this.OrderStatisticsList.find(
        v => v.periodsNum == this.orderInfo.periodsNum
      )
      console.log('orderSelectList---', orderSelectList)
      console.log('twoSingleSectionsList---', twoSingleSectionsList)
      console.log('fourSingleSectionsList---', fourSingleSectionsList)
      // return
      if (orderSelectList && orderSelectList.winningNumber && orderSelectList.winningNumber.length > 0) {
        for (let i = 0; i < orderSelectList.winningNumber.length; i++) {
          // 开奖号码
          const winningNumber = orderSelectList.winningNumber[i]
          let v
          // 两位订单列表
          for (let key = 0; key < twoSingleSectionsList.length; key++) {
            v = twoSingleSectionsList[key]
            let orderNum = v.orderNum
            let money = v.quantity * this.OrderSetting.quantityPrice_2
            let winFilter1 = 0
            // 对后两位
            winFilter1 = this.winFilter(0, i, winningNumber, orderNum, money)
            moneyWin += winFilter1

            console.log('小标--对后两位',orderNum,winFilter1)
          }
          // 四位订单列表
          for (let key2 = 0; key2 < fourSingleSectionsList.length; key2++) {
            v = fourSingleSectionsList[key2]
            let orderNum = v.orderNum
            let money = v.quantity * this.OrderSetting.quantityPrice_1
            let winFilter2 = 0
            let winFilter3_1 = 0
            let winFilter3_2 = 0
            let winFilter4_1 = 0
            let winFilter4_2 = 0
            let winFilter5 = 0
            let check = false

            // 大标4位全对
            winFilter2 = this.winFilter(1, i, winningNumber, orderNum, money)
            if (winFilter2) {
              check = true
              moneyWin += winFilter2
            }

            // 大标中前3 或者 后3
            if (!check) {
              winFilter3_1 = this.winFilter(21, i, winningNumber, orderNum.substr(0, 3), money)
              winFilter3_2 = this.winFilter(22, i, winningNumber, orderNum.substr(-3, 3), money)
            }
            if (winFilter3_1 || winFilter3_2) {
              check = true
              moneyWin += winFilter3_1
              moneyWin += winFilter3_2
            }
            
            // 大标 中 前后两位
            if (!check) {
              // console.log(i)
              if (i == 0) {
                winFilter4_1 = this.winFilter(31, i, winningNumber, orderNum.substr(0, 2), money)
              }
              winFilter4_2 = this.winFilter(32, i, winningNumber, orderNum.substr(2, 2), money)
            }
            if (winFilter4_1 || winFilter4_2) {
              check = true
              moneyWin += winFilter4_1
              moneyWin += winFilter4_2
            }

            // 中最后一位
            if (!check && i == 0) {
              winFilter5 = this.winFilter(4, i, winningNumber, orderNum.substr(-1, 4), money)
              moneyWin += winFilter5
            }
            // console.log('-----------------------------------------------------')
            // console.log(`${i+1}标--${winningNumber}`+'大标--4位全对','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter2)
            // console.log(`${i+1}标--${winningNumber}`+'大标--中前3','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter3_1)
            // console.log(`${i+1}标--${winningNumber}`+'大标--中后3','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter3_2)
            // console.log(`${i+1}标--${winningNumber}`+'大标--中前两位','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter4_1)
            // console.log(`${i+1}标--${winningNumber}`+'大标--中后两位','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter4_2)
            // console.log(`${i+1}标--${winningNumber}`+'大标--中最后一位','下单号码：'+orderNum,'购买金额：'+money,'中奖金额：'+winFilter5)
            // console.log('-----------------------------------------------------')
          }
        }
      }
      console.log('中奖总计--------', moneyWin)
      this.moneyWin = moneyWin
      if (moneyWin > 0) status = '已中奖'
      return status
    },

    orderNumQuantity() {
      let data = ''
      for (let i = 0; i < this.orderInfo.orderNumList.length; i++) {
        const item = this.orderInfo.orderNumList[i];
        data += `${item} * ${this.orderInfo.quantityiLst[i]}${i != this.orderInfo.orderNumList.length - 1 ? ', ' : ''}`
      }
      return data
    },
  },
  methods: {
    // 期数数据初始化
    OrderStatisticsInit() {
      uni.getStorage({
        key: 'OrderStatistics',
        success: (res) => {
          this.OrderStatisticsList = [
            ...this.OrderStatisticsList,
            ...res.data.map(v => {
              return v
            })
          ]
        }
      })
      // 拼接当前期数数据
      getOrderList(data => {
        this.OrderStatisticsList.push({
          orderList: data,
          periodsNum: this.AppSetting.periodsNum,
          drawingDate: this.OrderSetting.drawingDate,
          orderAmount: ''
        })
      })
    },

    // 中奖判断
    winFilter(type, ruleType, winningNumber, orderNum, money) {
      // 中奖规则
      let winningNumberRule = [
        [
          1 * 56, // 小标对后两位
          1 * 2000, // 大标4位全对
          1 * 50, // 大标中前3 或者 后3
          1 * 3, // 大标，小标 中 前后两位
          1 * 1 // 中最后一位
        ],
        [
          1 * 12, // 小标对后两位
          1 * 600, // 大标4位全对
          1 * 20, // 大标中前3 或者 后3
          1 * 2, // 大标，小标 中 前后两位
          0 // 中最后一位
        ],
        [
          1 * 8, // 小标对后两位
          1 * 300, // 大标4位全对
          1 * 10, // 大标中前3 或者 后3
          1 * 1, // 大标，小标 中 前后两位
          0 // 中最后一位
        ]
      ]
      // let winMoney = 0
      switch (type) {
        // 对后两位
        case 0:
          if (winningNumber.substr(-2, 4) == orderNum) {
            return winningNumberRule[ruleType][type] * money
          }
          return 0
        // 大标4位全对
        case 1:
          if (winningNumber == orderNum) {
            return winningNumberRule[ruleType][type] * money
          }
          return 0
        // 大标中前3 或者 后3
        case 21:
          if (winningNumber.substr(0, 3) == orderNum) {
            return winningNumberRule[ruleType][2] * money
          }
          return 0
        case 22:
          if (winningNumber.substr(-3, 3) == orderNum) {
            return winningNumberRule[ruleType][2] * money
          }
          return 0
        // 大标，小标 前后两位
        case 31:
          if (winningNumber.substr(0, 2) == orderNum) {
            return winningNumberRule[ruleType][3] * money
          }
          return 0
        case 32:
          if(winningNumber.substr(2, 2) == orderNum) {
            return winningNumberRule[ruleType][3] * money
          }
          return 0
        // 中最后一位
        case 4:
          if (winningNumber.substr(-1, 4) == orderNum) {
            return winningNumberRule[ruleType][type] * money
          }
          return 0
        default:
          return 0
      }
    },

    timestampToTime: (timestamp) => {
      // 时间戳为10位需*1000，时间戳为13位不需乘1000
      var date = new Date(timestamp);
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h = date.getHours() + ":";
      var m = date.getMinutes() + ":";
      var s = date.getSeconds();
      return Y + M + D + h + m + s;
    },

    myClick(type, data) {
      switch (type) {
        case '打印订单':
          this.$refs.bluetoothPrintingPopup.open()
          setTimeout(() => {
            this.$refs.bluetoothPrinting.blueadapterCheck()
          }, 500)
          break;
        default:
          break;
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.orderDetail {
  .uni-input-text {
    width: 11rem;
  }
  .orderNumList-item {
    margin-right: 0.5rem;
    white-space: normal;
    word-break: break-all;
  }
}
</style>
